class Graph():
    def __init__(self, Adj = [], Vnames = None):
        self.Adj = Adj
        self.n = len(self.Adj)
        self.V = xrange(self.n)
        self.Vn = Vnames if len(Vnames) == self.n else None
        self.construct_from_Adj()
    def construct_from_Adj(self): #construct x,y,w,m
        self.m = 0
        self.x = dict()
        self.y = dict()
        self.w = dict()               
        for u in self.V:
            for r in Adj[u]:
                if type(r) == type((0,0)): #weighted graph
                    v, ww = r
                    m = self.m
                    self.x[m] = u
                    self.y[m] = v
                    self.w[m] = ww
                else: #unweighted graph
                    v = r
                    m = self.m
                    self.x[m] = u
                    self.y[m] = v
                self.m += 1
    def print_edges(self):
        for e in xrange(self.m):
            if self.Vn is None:
                print self.x[e], self.y[e]
            else:
                print self.Vn[self.x[e]], self.Vn[self.y[e]]

if __name__ == '__main__':
    Adj = [[2,1],[],[1,3],[]]
    G = Graph(Adj, ['a','b','c','d'])
    G.print_edges()

